package com.shuoyue.ycgk.ui.questionbank.search;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseViewHolder;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.base.baseadapter.AppBaseQuickAdapter;
import com.shuoyue.ycgk.entity.Option;
import com.shuoyue.ycgk.entity.QuestionSimple;
import com.shuoyue.ycgk.utils.MyRichTextHelper;
import com.shuoyue.ycgk.utils.SizeUtil;
import com.shuoyue.ycgk.utils.SpacesItemDecoration;

import java.util.List;

public class QuestionSearchAdapter extends AppBaseQuickAdapter<QuestionSimple> {
    public QuestionSearchAdapter(@Nullable List<QuestionSimple> data) {
        super(R.layout.item_question_search, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, QuestionSimple item) {
        helper.addOnClickListener(R.id.collection);
        helper.addOnClickListener(R.id.check_aly);

        String type = "";    /**
         * 题目类型 1:单选,2多选,3判断
         */
        switch (item.getQuestionType()) {
            case 1:
                type = "单选";
                break;
            case 2:
                type = "多选";
                break;
            case 3:
                type = "判断";
                break;
        }
        helper.setText(R.id.type, (helper.getLayoutPosition() + 1) + "." + type);
        MyRichTextHelper.showRich(mContext, item.getStem(), helper.getView(R.id.title));
        MyRichTextHelper.showRich(mContext, item.getAnalysis(), helper.getView(R.id.aly));
        if (item.getQuestionType() == 3) {
            if ("1".equals(item.getRightKey())) {
                helper.setText(R.id.right, "正确");

            } else if ("2".equals(item.getRightKey())) {
                helper.setText(R.id.right, "错误");

            } else {
                helper.setText(R.id.right, "");

            }
        } else {
            helper.setText(R.id.right, item.getRightKey());
        }

        helper.setGone(R.id.lay_aly, item.isShowRight());
        helper.setGone(R.id.check_aly, !item.isShowRight());

        helper.setVisible(R.id.right, item.isShowRight());
        helper.setImageResource(R.id.collection, item.getIsCollect() == 1 ? R.mipmap.collection_y : R.mipmap.collection_n);
        RecyclerView recyclerView = helper.getView(R.id.recycleOption);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        if (recyclerView.getTag() == null) {
            recyclerView.addItemDecoration(new SpacesItemDecoration(SizeUtil.dip2px(mContext, 8), false));
            recyclerView.setTag("tag");
        }
        if (item.getQuestionOptions() != null) {
            for (Option option : item.getQuestionOptions()) {
                if (item.getRightKey() != null && item.getRightKey().contains(option.getNo())) {
                    if (item.isShowRight()) {
                        option.setRight(true);
                    } else {
                        option.setRight(false);
                    }
                } else {
                    option.setRight(false);
                }
            }
        }
        SearchoptionAdapter childAdapter = new SearchoptionAdapter(item.getQuestionOptions());
        recyclerView.setAdapter(childAdapter);
    }
}
